package moduloIndexacion.analizador;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.Iterator;
import java.util.Vector;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import entidades.Extension;

public class AnalizadorExcel_xlsx extends TemplateAnalizador{
	
	public AnalizadorExcel_xlsx(){
		super.extension = new Extension ("xlsx","Microsoft Excel Versiones Post 2007");
	}
	
	@Override
	protected String obtenerTexto() {
		String rutaAcceso = documento.getUbicacion()+documento.getNombre();
		String textoExcel="";
		Vector<Vector<XSSFCell>> cellVectorHolder = new Vector<Vector<XSSFCell>>();
		
		InputStream inputStream = null; 
		
		try 
		{ 
			inputStream = new FileInputStream(rutaAcceso); 
		} 
		catch (FileNotFoundException e) 
		{ 
			System.out.println("File not found in the specified path."); 
			e.printStackTrace(); 
		} 
		try 
		{ 
			XSSFWorkbook workBook = new XSSFWorkbook(inputStream);
			for(int i=0; i<workBook.getNumberOfSheets(); i++)
			{
				XSSFSheet sheet = workBook.getSheetAt(i); 
				Iterator<Row> rowIter = sheet.rowIterator();
				while(rowIter.hasNext())
				{
					XSSFRow myRow = (XSSFRow) rowIter.next();
					Iterator<Cell> cellIter = myRow.cellIterator();
					Vector<XSSFCell> cellStoreVector=new Vector<XSSFCell>();
		              while(cellIter.hasNext()){
		                  XSSFCell myCell = (XSSFCell) cellIter.next();
		                  cellStoreVector.addElement(myCell);
		              }
		              cellVectorHolder.addElement(cellStoreVector);
				}
			}
			inputStream.close();
		} 
		catch (Exception e) {
			System.out.println("ERROR: "+e.toString());
			return null;
		}

		for (int i=0;i<cellVectorHolder.size(); i++)
		{
			Vector<XSSFCell> cellStoreVector=(Vector<XSSFCell>)cellVectorHolder.elementAt(i);
			for (int j=0; j < cellStoreVector.size();j++)
			{
		         XSSFCell myCell = (XSSFCell)cellStoreVector.elementAt(j);
		         String stringCellValue = myCell.toString();
		         //System.out.print(stringCellValue+"\t");
		         textoExcel += " "+stringCellValue;
			}     						
		
		}
		return textoExcel;

	}
}
